package cn.golxen.rsms.config;

import cn.golxen.rsms.filter.WebFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WebConfig {
    @Bean
    public FilterRegistrationBean<WebFilter> firstFilter(){
        FilterRegistrationBean<WebFilter> filterRegBean=new FilterRegistrationBean<>();
        filterRegBean.setFilter(new WebFilter());
        filterRegBean.addUrlPatterns("*");
        String path="/store/sendRegisterMail,"
                +"/store/registerUser,"
                +"/store/loginUser,"
                +"/store/loginUserOnSendMail,"
                +"/store/get/userMainByOpenId/,"
                +"/store/get/OpenIdByWxCode/,"
                +"/store/get/storeList/,"
                +"/goods/get/goodsInfo2/,"
                +"/goods/get/goodsCodeList/," +
                "/goods/get/typeList," +
                "/goods/get/newGoodsStore/," +
                "/goods/get/goodsHotSale/," +
                "/goods/get/goodsUnsalable/," +
                "/store/get/StoreOffersListRuing/";
        filterRegBean.addInitParameter("excludedUris",path);
        filterRegBean.setOrder(1);
//        filterRegBean.addInitParameter("checkUri","/admin/check");
        return filterRegBean;
    }

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置你要允许的网站域名
        config.addAllowedOrigin("*");
//        config.addAllowedOrigin("http://rsms.golxen.com");
        //允许跨域发送cookie
//        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
